<template>
  <div class="app-container">
    <!-- 筛选框 -->
    <el-form :inline="true">
      <el-form-item>
        <el-input v-model="search_form.adName" name="adName" placeholder="请输入轮播图名称" />
      </el-form-item>
      <el-form-item>
        <el-select v-model="search_form.status" placeholder="请选择上架状态">
          <el-option label="全部" value="" />
          <el-option label="上架" value="1" />
          <el-option label="下架" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-cascader
          v-model="search_form.provinceId"
          placeholder="请选择省份"
          :options="addressOptions"
          :props="propOption"
          @change="handleChange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain class="el-icon-search" @click="search">查询</el-button>
        <el-button type="primary" plain class="el-icon-refresh" @click="reset">重置</el-button>
      </el-form-item>
      <el-form-item style="float:right">
        <el-button
          type="primary"
          plain
          class="el-icon-plus"
          @click="showMask"
        >添加</el-button>
      </el-form-item>
    </el-form>

    <!-- 轮播图列表 -->
    <div class="tableWrap">
      <el-table v-loading="table_loading" :data="tableData" border style="width: 100%">
        <!-- id -->
        <el-table-column prop="sort" label="排序" align="center" />
        <!-- 轮播图名称 -->
        <el-table-column prop="adName" label="轮播图名称" align="center" />
        <!-- 轮播图图片 -->
        <el-table-column label="轮播图图片" align="center">
          <template slot-scope="scope">
            <div>
              <img :src="common.img_url+scope.row.adPicture" alt class="smallBG">
            </div>
          </template>
        </el-table-column>

        <!-- 省份 -->
        <el-table-column prop="province" label="省份" align="center" />

        <!-- 上架状态 -->
        <el-table-column label="上架状态" align="center">
          <template slot-scope="scope">
            <div>
              <el-switch
                v-model="scope.row.status"
                active-text="上架"
                inactive-text="下架"
                :active-value="1"
                :inactive-value="0"
                @change="changeSwitch($event,scope.row)"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column :formatter="jumpFormat" label="跳转链接" align="center" />
        <!-- 操作 -->
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="editAdv(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="deleteAdv(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </div>

    <!-- 添加的页面 -->
    <adv-add
      v-if="showAddMask"
      :is-type="type"
      :is-hidden="showAddMask"
      :detail-info="detailInfo"
      :is-disabled="isDisabled"
      :address="addressOptions"
      @checkMask="checkMask"
      @refresh="addOrUpdate"
    />

    <!-- 分页器 -->
    <div class="block">
      <el-pagination
        background
        :page-sizes="[10,20,50,100]"
        :page-size="pageSize"
        :pager-count="11"
        layout="total, sizes, prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>

  </div>
</template>
<script>
import advAdd from '@/components/adv/adv_add'
import { provinceList } from '../../api/province'
import { deleteAd, getAdList, updateAd } from '../../api/adv'
import { getGoodsList } from '../../api/goods'
import { projectList } from '../../api/project'
import { appType } from '../../utils/constanst' // 添加组件
export default {
  components: {
    advAdd
  },
  data() { // 数据声明初始
    return {
      adName: null,
      status: null,
      addressOptions: [], // 省份选择 options
      propOption: {
        value: 'id',
        label: 'name'
      }, // 省份选择 props
      search_form: {
        adName: null,
        status: null,
        provinceId: []
      }, // 筛选条件
      provinceId: [], // 省份id？
      type: 0, // 0-轮播图
      showAddMask: false, // 是否显示添加弹窗
      page: 1,
      pageSize: 10,
      total: 0,
      tableData: [], // 列表数据
      goodsData: [],//礼包列表
      projectData: [],//项目列表
      detailInfo: {
      }, // 详细信息-修改
      isDisabled: false, // 默认允许是否编辑
      table_loading: false,
      model: 0// 0 初次加载   1表示条件筛选
    }
  },
  //新加路由保存参数
  beforeRouteLeave(to, from, next) {
    // const answer = window.confirm("当前页面搜索条件将会被保存");
    //更新 put
    let keyname = this.$route.name;
    let value = JSON.stringify(this.search_form);
    sessionStorage.setItem(keyname, value);
    next();
  },

  // 生命周期钩子，初始时获取数据
  mounted() {
    // 获取数据列表
    if (sessionStorage.getItem(this.$route.name)) {
      this.search_form = JSON.parse(sessionStorage.getItem(this.$route.name));
    }
    this.search()
    // 获取省option
    this.get_province()
  },
  methods: {
    //数据处理 --跳转链接
    jumpFormat(row) {
      if (row.jumpUrl === 'card:1') {
        return '会员卡'
      } else if (row.jumpUrl.indexOf('id:') === 0) {
        return '项目详情/' + row.jumpName
      } else if (row.jumpUrl.indexOf('goods:') === 0) {
        return '礼包详情/' + row.jumpName
      }else if (row.jumpUrl.indexOf('applet:') === 0) {
        return '小程序/' + row.jumpName
      }
      else {
        return row.jumpUrl
      }
    },
    // 省份选择器change事件
    handleChange(val) {
      if (val.lenght > 0) {
        this.provinceId = val[0]
      }
    },
    // 筛选
    search() {
      this.model = 1
      this.getSlideshowLists({
        adName: this.search_form.adName ? this.search_form.adName : null,
        status: parseInt(this.search_form.status) === 0 ? 0 : parseInt(this.search_form.status),
        provinceId: this.search_form.provinceId[0],
        pageSize: this.pageSize,
        pageNum: this.page,
        positionType: 0
      })
    },
    // 重置
    reset() {
      this.page = 1
      this.pageSize = 10
      this.search_form.adName = ''
      this.search_form.status = ''
      this.search_form.provinceId = []
      this.getSlideshowLists()
    },
    // 获取数据列表
    getSlideshowLists(options = {
      pageSize: this.pageSize,
      pageNum: this.page,
      positionType: 0
    }) {
      this.table_loading = true
      getAdList(options).then(res => {
        this.total = res.data.data.count
        this.pageNum = res.data.data.pageNum
        this.pageSize = res.data.data.pageSize
        this.tableData = res.data.data.list
        this.table_loading = false
      }).catch((error) => {
        this.table_loading = false
        this.$message.error('网络连接错误，请稍后再试')
      })
    },
    // 添加
    showMask() {
      this.detailInfo = {} // 详细信息初始为空
      this.showAddMask = true // 显示添加弹窗
    },
    // 删除
    deleteAdv(id) {
      this.$confirm('确认删除该轮播图吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAd({
          id: id
        }).then((result) => {
          const res = result.data
          if (res.code === 0) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.search()
          } else {
            this.$message.error(res.msg)
          }
        }).catch((err) => {
          this.$message.error('服务器网络错误，请稍后重试')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑
    editAdv(row) {
      this.detailInfo = row // 赋值
      this.isDisabled = false // 能否编辑
      this.showAddMask = true // 显示添加弹窗
    },
    //添加或更新
    addOrUpdate(){
      this.search()
    },
    //关闭弹窗
    checkMask(isHidden) {
      this.detailInfo = {}
      this.showAddMask = isHidden
    },
    // 上架状态改变
    changeSwitch(status, item) {
      updateAd( {
        id: item.id,
        status: status
      }).then(res => {
        if (res.data.code=== 0) {
          this.$message({
            message: '状态修改成功',
            type: 'success'
          })
        } else {
          this.$message.error(res.data.msg)
        }
        // 刷新列表
        this.search()
      }).catch(res => {
        this.$message.error('网络连接错误，请稍后再试')
      })
    },
    //获取省份
    get_province(){
      provinceList({}).then(res => {
        this.addressOptions = res.data.data.list // 将获取的省份option 赋值 addressOptions
      }).catch(res => {
        this.$message.error('网络连接错误，请稍后再试')
      })
    },
    //页码改变
    handleCurrentChange(page) {
      this.page = page
      this.search()
    },
    //条数改变
    handleSizeChange(val) {
      this.pageSize = val
      this.page = 1
      this.search()
    },
  }
}
</script>
<style scoped>
  label {
    font-size: 14px;
    font-weight: normal;
  }
  .smallBG {
    width: 100px;
    height: 100px;
  }
  .tableWrap {
    margin-top: 20px;
  }
  .block {
    background: #fff;
    padding: 32px 16px;
  }
</style>
